package com.xxx.exception;

import com.xxx.common.model.RespEnum;
import com.xxx.json.GsonUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

;
/**
 * @author Flying
 * @Description
 * @date 12:44 2018-08-27
 **/

@Slf4j
public class WechatException extends RuntimeException{


    @Getter
    private Integer code;

    @Getter
    private String message;


    /**
     * @param description 描述
     */
    public WechatException(String description){
        super(description);
        this.code = RespEnum.SERVER_ERROR.getCode();
        this.message = description;
        if(log.isInfoEnabled()) {
            log.info(this.code + ":" + this.message);
        }
    }

    /**
     * @param respEnum 枚举信息
     */
    public WechatException(RespEnum respEnum){
        super(respEnum.getDesc());
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc());
        }
    }

    /**
     *
     * @param respEnum 枚举信息
     * @param e 异常
     */
    public WechatException(RespEnum respEnum, Throwable e){
        super(respEnum.getDesc(),e);
        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage(), e);
        }
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
    }
    /**
     *
     * @param respEnum 枚举信息
     * @param arguments 参数
     */
    public WechatException(RespEnum respEnum, Object arguments){
        super(respEnum.getDesc());
        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc() + "|arguments:" + GsonUtil.GsonString(arguments));
        }
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
    }

    /**
     *
     * @param respEnum 枚举信息
     * @param throwable 异常
     * @param arguments 参数
     */
    public WechatException(RespEnum respEnum, Object  arguments, Throwable e){
        super(respEnum.getDesc(),e);
        if(log.isInfoEnabled()) {
            log.info(respEnum.getCode() + ":" + respEnum.getDesc() + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + GsonUtil.GsonString(arguments), e);
        }
        this.code = respEnum.getCode();
        this.message = respEnum.getDesc();
    }

    /**
     *
     */
    public WechatException(Integer code, String description, Throwable e){
        super(description,e);
        if(log.isInfoEnabled()) {
            log.info(code + ":" + description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
        }
        this.code = RespEnum.SERVER_ERROR.getCode();
        this.message =  RespEnum.SERVER_ERROR.getDesc();
    }

    /**
     *
     */
    public WechatException(String description, Throwable e){
        super(description,e);
        if(log.isInfoEnabled()) {
            log.info(description + "|cause:" + e.getCause() + "|message:" + e.getMessage() + "|arguments:" + e);
        }
        this.code = RespEnum.SERVER_ERROR.getCode();
        this.message =  RespEnum.SERVER_ERROR.getDesc();
    }



}
